# load packages
library("rvest")
Warning: package ‘rvest’ was built under R version 4.1.2
library("tibble")
library("syuzhet")
Warning: package ‘syuzhet’ was built under R version 4.1.2
library("sentimentr")
Warning: package ‘sentimentr’ was built under R version 4.1.2
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Attaching package: ‘sentimentr’
The following object is masked from ‘package:syuzhet’:
get_sentences
library("gplots")
Warning: package ‘gplots’ was built under R version 4.1.2
Attaching package: ‘gplots’
The following object is masked from ‘package:stats’:
lowess
library("plyr")
library("dplyr")
library("tm")
Warning: package ‘tm’ was built under R version 4.1.2
Loading required package: NLP
Warning: package ‘NLP’ was built under R version 4.1.1
library("syuzhet")
library("factoextra")
Warning: package ‘factoextra’ was built under R version 4.1.2
Loading required package: ggplot2
Attaching package: ‘ggplot2’
The following object is masked from ‘package:NLP’:
annotate
Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library("beeswarm")
Warning: package ‘beeswarm’ was built under R version 4.1.1
library("scales")
Attaching package: ‘scales’
The following object is masked from ‘package:syuzhet’:
rescale
library("RColorBrewer")
library("RANN")
Warning: package ‘RANN’ was built under R version 4.1.2
library("tm")
library("topicmodels")
Warning: package ‘topicmodels’ was built under R version 4.1.2
library("stringr")
writeLines(as.character(docs[[sample(1:nrow(corpus.list), 1)]]))
A tenant at will, who possesses land sufficient to maintain his family for little more than a quit rent, is as dependent upon the proprietor as any servant or retainer whatever, and must obey him with as little reserve. Such a proprietor, as he feeds his servants and retainers at his own house, so he feeds his tenants at their houses. The subsistence of both is derived from his bounty, and its continuance depends upon his good pleasure.
Adapted from https://eight2late.wordpress.com/2015/09/29/a-gentle-introduction-to-topic-modeling-using-r/.
set.seed(1)
index <- sample(1:nrow(corpus.list), 1)
print("Sample sentence:")
[1] "Sample sentence:"
writeLines(as.character(docs[[index]]))
Prison Reform: for the benefit of the working class. This is the last word and the only seriously meant word of bourgeois Socialism. It is summed up in the phrase: the bourgeois is a bourgeois for the benefit of the working class.
print("After converting to lower case:")
[1] "After converting to lower case:"
#remove potentially problematic symbols
docs <-tm_map(docs,content_transformer(tolower))
writeLines(as.character(docs[[index]]))
prison reform: for the benefit of the working class. this is the last word and the only seriously meant word of bourgeois socialism. it is summed up in the phrase: the bourgeois is a bourgeois for the benefit of the working class.
print("After removing punctuation:")
[1] "After removing punctuation:"
#remove punctuation
docs <- tm_map(docs, removePunctuation)
writeLines(as.character(docs[[index]]))
prison reform for the benefit of the working class this is the last word and the only seriously meant word of bourgeois socialism it is summed up in the phrase the bourgeois is a bourgeois for the benefit of the working class
print("After removing numbers:")
[1] "After removing numbers:"
#Strip digits
docs <- tm_map(docs, removeNumbers)
writeLines(as.character(docs[[index]]))
prison reform for the benefit of the working class this is the last word and the only seriously meant word of bourgeois socialism it is summed up in the phrase the bourgeois is a bourgeois for the benefit of the working class
print("After removing stopwords:")
[1] "After removing stopwords:"
#remove stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
writeLines(as.character(docs[[index]]))
prison reform benefit working class last word seriously meant word bourgeois socialism summed phrase bourgeois bourgeois benefit working class
print("After removing whitespace:")
[1] "After removing whitespace:"
#remove whitespace
docs <- tm_map(docs, stripWhitespace)
writeLines(as.character(docs[[index]]))
prison reform benefit working class last word seriously meant word bourgeois socialism summed phrase bourgeois bourgeois benefit working class
print("After stemming:")
[1] "After stemming:"
#Stem document
docs <- tm_map(docs,stemDocument)
writeLines(as.character(docs[[index]]))
prison reform benefit work class last word serious meant word bourgeoi social sum phrase bourgeoi bourgeoi benefit work class
Generate document-term matrices.
dtm <- dtm[rowTotals> 0, ]
Error: unexpected symbol in:
" rowTotals[(5000*(i-1)+1):nrow(dtm)] <- c(rowTotals,apply(dtm[(5000*(i-1)+1):nr
dtm"
Run LDA
# DO NOT RUN FOR EVALUATION. THE REQUIRED OUTPUTS ARE SAVED AS CSV FILES FOR EASY ACCESS.
#Set parameters for Gibbs sampling
burnin <- 4000
iter <- 2000
thin <- 500
seed <-list(2003,5,63,100001,765)
nstart <- 5
best <- TRUE
#Number of topics
k <- 8
#Run LDA using Gibbs sampling
ldaOut <-LDA(dtm, k, method="Gibbs", control=list(nstart=nstart,
seed = seed, best=best,
burnin = burnin, iter = iter,
thin=thin))
#write out results
#docs to topics
ldaOut.topics <- as.matrix(topics(ldaOut))
table(c(1:k, ldaOut.topics))
write.csv(ldaOut.topics,file=paste("../output/LDAGibbs",k,"DocsToTopics.csv", sep="_"))
#top 8 terms in each topic
ldaOut.terms <- as.matrix(terms(ldaOut,8))
write.csv(ldaOut.terms,file=paste("../output/LDAGibbs",k,"TopicsToTerms.csv", sep="_"))
#probabilities associated with each topic assignment
topicProbabilities <- as.data.frame(ldaOut@gamma)
write.csv(topicProbabilities,file=paste("../output/LDAGibbs",k,"TopicProbabilities.csv", sep="_"))
downloads <- rbind(
c("The Prince", "Machiavelli", 14217),
c("The Republic", "Plato", 8856 ),
c("Second Treatise of Government", "Locke",6860),
c("Beyond Good and Evil", "Nietzsche", 6427),
c("Thus Spake Zarathustra: A Book for All and None", "Nietzsche", 5276),
c("Tractatus Logico-Philosophicus", "Wittgenstein", 5094),
c("The Communist Manifesto","Marx", 4631),
c("The Problems of Philosophy", "Russell", 4138),
c("On Liberty","Mill", 3526 ),
c("Democracy and Education: An Introduction to the Philosophy of Education","Dewey",2878),
c("The Critique of Pure Reason","Kant",2449 ),
c("The Poetics of Aristotle","Aristotle",2422 ),
c("Ethics","Spinoza",2290 ),
c("The Antichrist","Nietzsche",1765),
c("Apology","Plato",1655 ),
c("An Enquiry Concerning Human Understanding","Hume",1598 ),
c("Discourse on the Method of Rightly Conducting One's Reason and of Seeking Truth in the Sciences","Descartes",1552 ),
c("Euthyphro","Plato",1423 ),
c("Utilitarianism","Mill",1349 ),
c("A Treatise of Human Nature","Hume",1260 ),
c("The Ethics of Aristotle","Aristotle",1236 ),
c("Theaetetus","Plato",988),
c("Also sprach Zarathustra: Ein Buch für Alle und Keinen (German)","Nietzsche",984 ),
c("The Analysis of Mind","Russell",823 ),
c("An Enquiry Concerning the Principles of Morals","Hume",785),
c("Politics: A Treatise on Government","Aristotle",652 ),
c("Mysticism and Logic and Other Essays", "Russell",535 ),
c("Laughter: An Essay on the Meaning of the Comic","Bergson",528 ),
c("Considerations on Representative Government","Mill",516 ),
c("Ion","Plato",495 ),
c("Dialogues Concerning Natural Religion","Hume",479 ),
c("Symbolic Logic","Carroll",462 ),
c("Kritik der reinen Vernunft (German)","Kant",414 ),
c("The Case of Wagner, Nietzsche Contra Wagner, and Selected Aphorisms.","Nietzsche",367 ),
c("Aesthetical Essays of Friedrich Schiller","Schiller",339 ),
c("Our Knowledge of the External World as a Field for Scientific Method in Philosophy","Russell",331 ),
c("The Game of Logic","Carroll",323 ),
c("Aristotle on the art of poetry","Aristotle",319 ),
c("A Treatise Concerning the Principles of Human Knowledge","Berkeley",290 ),
c("A System of Logic, Ratiocinative and Inductive","Mill",273 ),
c("The Categories","Aristotle",272 ),
c("Theologico-Political Treatise — Part 1","Spinoza",258 ),
c("Moral Principles in Education","Dewey",253 ),
c("Discours de la méthode (French)","Descartes",249),
c("A System of Logic, Ratiocinative and Inductive (Vol. 1 of 2)","Mill",249),
c("A History of Indian Philosophy, Volume 1","Dasgupta",247 ),
c("Ontology, or the Theory of Being","Coffey",225 ),
c("On the Improvement of the Understanding","Spinoza",219 ),
c("A Guide to Stoicism","Stock",219 ),
c("Proposed Roads to Freedom","Russell",207 ),
c("Three Dialogues Between Hylas and Philonous in Opposition to Sceptics and Atheists","Berkeley",205),
c("Kritik der reinen Vernunft (German)","Kant",203 ),
c("Autobiography","Mill",197 ),
c("Political Ideals","Russell",189 ),
c("Logic: Deductive and Inductive","Read",159 ),
c("An Introduction to Philosophy", "Fullerton", 154),
c("Die Geburt der Tragödie: Versuch einer Selbstkritik (German)","Nietzsche",150 ),
c("The Religion of the Samurai","Nukariya",146 ),
c("Selections from the Principles of Philosophy","Descartes",143 ),
c("Essays in Radical Empiricism","James",139 ),
c("A Pluralistic Universe","James",136),
c("Sextus Empiricus and Greek Scepticism","Mills Patrick",110 ),
c("History of Modern Philosophy","Falckenberg",98),
c("Auguste Comte and Positivism","Mill",98 ),
c("Some Turns of Thought in Modern Philosophy: Five Essays","Santayana",96 ),
c("An Introduction to the Philosophy of Law","Pound",95 ),
c("Ethics — Part 1","Spinoza",73),
c("Essays Towards a Theory of Knowledge","Philip",71 ),
c("The Philosophy of Despair","Jordan",71 ),
c("A System of Logic: Ratiocinative and Inductive, 7th Edition, Vol. II","Mill",70),
c("Homer and Classical Philology","Nietzsche",69 ),
c("Philosophical Letters of Friedrich Schiller","Schiller",67 ),
c("The Psychology of Nations","Partridge",66 ),
c("Logic, Inductive and Deductive","Minto",65 ),
c("The Theological Tractates and The Consolation of Philosophy (Latin)","Boethius",64 ),
c("Theologico-Political Treatise — Part 2", "Spinoza",62 ),
c("A Theological-Political Treatise [Part III]","Spinoza",56 ),
c("Bacon", "Church",55 ),
c("A System of Logic: Ratiocinative and Inductive, 7th Edition, Vol. I","Mill",54 ),
c("Introduction to the Philosophy and Writings of Plato","Taylor",54 ),
c("The Philosophy of the Moral Feelings","Abercrombie",54 ),
c("A Short History of Greek Philosophy","Marshall",53 ),
c("Jewish History : An Essay in the Philosophy of History","Dubnow",53 ),
c("A Theological-Political Treatise [Part IV]","Spinoza",51 ),
c("Essays on some unsettled Questions of Political Economy","Mill",50 ),
c("Initiation into Philosophy","Faguet",49 ),
c("Deductive Logic","Stock",49 ),
c("Analysis of Mr. Mill's System of Logic","Stebbing",48 ),
c("A New Philosophy: Henri Bergson","Le Roy",47 ),
c("Lectures on the true, the beautiful and the good","Cousin",46 ),
c("Mind and Motion and Monism","Romanes",44 ),
c("The Principles of Aesthetics","Parker",44 ),
c("Ethics — Part 3","Spinoza",43 ),
c("Bergson and His Philosophy","Gunn",41 ),
c("Ethics — Part 4","Spinoza",38 ),
c("Ethics — Part 5","Spinoza",37 ),
c("Rudolph Eucken : a philosophy of life","Jones",36 ),
c("Review of the Work of Mr John Stuart Mill Entitled, 'Examination of Sir William Hamilton's Philosophy.'","Grote",36 ),
c("Ethics — Part 2","Spinoza",34 ),
c("Philosophy and Religion","Rashdall",34 ),
c("The English Utilitarians, Volume 1 (of 3)","Stephen",33 ),
c("An Interpretation of Rudolf Eucken's Philosophy","Jones",32 ),
c("Modern French Philosophy: a Study of the Development Since Comte","Gunn",31 ),
c("Burke","Morley",29 ),
c("The Approach to Philosophy","Perry",28 ),
c("Achtundvierzig Briefe von Johann Gottlieb Fichte und seinen Verwandten (German)","Fichte",21 ),
c("Critical Miscellanies (Vol. 3 of 3), Essay 10: Auguste Comte","Morley",20 ),
c("Critical Miscellanies, (Vol. 3 of 3), Essay 2: The Death of Mr Mill; Essay 3: Mr Mill's Autobiography","Morley",19 )
)
downloads <- data.frame(downloads)
colnames(downloads) <- c("Title", "Author", "downloads")
downloads$downloads <- as.integer(downloads$downloads)
phil %>% group_by(title, author) %>% summarize(s = n())
Mind and body. Cartesian - duality between mind and body. justice, good, mind. male vs female, feminism - wollstonecraft. naturalism. religion, god. Deontolgy and consequentialsm. location. free will. society vs self. females focused more on society
good harm action